<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Event/classes/PhpFox_ComponentEventAddComments.class.php, $Author:  $ */



/** Component for adding event comments
 * @version $Id: PhpFox_ComponentEventAddComments.class.php 422 2008-04-01 13:30:10Z phpfoxsv $
 * @package module.event
 *
 */
class PhpFox_Mod_Event_ComponentEventAddComments extends BaseComponent
{
    function process()
    {
    	if ( !App::getUser('user') )
    	{
    		return '';
    	}

        $oEventInviteItem = $this->_oModule->getItem('EventInvite');
        /* @var $oEventInviteItem PhpFox_Mod_Event_ItemEventInvite */
        $oEventItem = $this->_oModule->getItem('Event');
        /* @var $oEventItem PhpFox_Mod_Event_ItemEvent */
        $oEventService = $this->_oModule->getService('Event');
        /* @var $oEventService PhpFox_Mod_Event_ServiceEvent */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oBlockService = &App::getModuleService('Account', 'Block');
        /* @var $oBlockService PhpFox_Mod_Account_ServiceBlock */
        $oTpl = $this->_oModule->getTemplate();
        $oReq = &App::getRequest();
        App::loadClass('Text');
        $oTxt = &new Text();

        if (!$oSecurityService->checkPerm('events', array('full'), false))
        {
            return '';
        }
        $oSecurityService->checkAccess('events');
        $iEventId = $oReq->getInt('id');
        if (!$iEventId)
        {
            App::gotoUrl('public.event.main');
        }
        if (!$oEventService->isEventCanViewed($iEventId))
        {
            App::gotoUrl('public.event.main');
        }
        if (!$oEventItem->load($iEventId))
        {
            App::gotoUrl('public.event.main');
        }
        $oOwner = $oSecurityService->getUserByName($oEventItem->aData['user']);
        $oUser = $oSecurityService->getCurrentUser();
        $sUser = $oUser->aData['user'];
        if ($oReq->getAction() == 'comment')
        {
            if ($oBlockService->isBlocked($oOwner, $oUser))
            {
                App::gotoUrl('public.event.view', array('id' => $iEventId), App::format('Mod_Event.error.blocked'), true, 'add_comment');
            }
            $sText = $oReq->get('comment');
            if (!$sText)
            {
                App::gotoUrl('public.event.view', array('id' => $iEventId), App::format('Mod_Event.error.no_text'), true, 'add_comment');
            }
            $oEventInviteItem->aData['user']     = $sUser;
            $oEventInviteItem->aData['event_id'] = $iEventId;
            $oEventInviteItem->aData['text']     = $oTxt->preParseText($sText);
            $oEventInviteItem->aData['invite']   = 0;
            $oEventInviteItem->aData['time']     = time();
            $oEventInviteItem->aData['type']     = 1;
            $oEventInviteItem->insert();

            if (($sUser != $oOwner->aData['user']) && ($oOwner->aData['not_1'] != 0))
            {
                $oMailer = &App::newObject('mail_Mailer');
                /* @var $oMailer mail_Mailer */
                $sSubject = App::format('core.comment');
                $sTo = $oOwner->aData['email'];
                $aVars = array
                (
                    'TO'           => $oOwner->aData['user'],
                    'FROM'         => $sUser,
                    'SITENAME'     => App::getParam('url'),
                    'HOME'         => App::getParam('url'),
                    'COMMENT_LINK' => App::makeUrl('public.event.view', array('id' => $iEventId)),
                );
                $oMailer->send('email_comment', $sTo, $sSubject, $aVars);
            }

            App::gotoUrl('public.event.view', array('id' => $iEventId));
        }

        $oTpl->assign(array(
        	'iEventId'   => $iEventId,
        	'bLogged'    => ($sUser) ? true : false,
        	'useAjax' => App::getParam('useAjax'),
        ));
        return $oTpl->fetch('EventAddComments.html');
    }
}
?>